package hn.cch.spring_boot_rest_quartz.controller;


import hn.cch.spring_boot_rest_quartz.entity.QuartzTrigger;
import hn.cch.spring_boot_rest_quartz.result.QuartzMessage;
import hn.cch.spring_boot_rest_quartz.runner.QuartzRunner;
import hn.cch.spring_boot_rest_quartz.service.QuartzService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class QuartzController {

    public static Logger logger = LoggerFactory.getLogger(QuartzController.class);

    @Autowired
    private QuartzService quartzService;


    @RequestMapping(value = "/getAllJob", method = RequestMethod.GET)
    @ResponseBody
    public QuartzMessage getAllJob() {
        logger.info("url:getAllJob");
        List<QuartzTrigger> allJob = quartzService.getAllJob();
        logger.info("allJob:" + allJob.size());
        return QuartzMessage.success().add("allJob", allJob);
    }


    @RequestMapping(value = "/getJob", method = RequestMethod.GET)
    @ResponseBody
    public QuartzMessage getJob(@RequestParam("jobName") String jobName, @RequestParam("jobGroup") String jobGroup) {
        logger.info("url:getJob");
        QuartzTrigger job = quartzService.getJob(jobName, jobGroup);
        logger.info("job :" + job);
        return QuartzMessage.success().add("job", job);
    }

    @RequestMapping(value = "/scheduleJob", method = RequestMethod.POST)
    @ResponseBody
    public QuartzMessage scheduleJob(@Valid QuartzTrigger quartzTrigger, BindingResult bindingResult) {
        logger.info("url:scheduleJob");
        if (bindingResult.hasFieldErrors()) {
            Map<String, Object> map = new HashMap<>();
            List<FieldError> fieldErrors = bindingResult.getFieldErrors();
            for (FieldError fieldError : fieldErrors) {
                logger.info("错误字段：" + fieldError.getField());
                logger.info("错误信息：" + fieldError.getDefaultMessage());
                map.put(fieldError.getField(), fieldError.getDefaultMessage());
            }
            return QuartzMessage.failed().add("fieldErrors", map);
        }

        if (quartzService.scheduleJob(quartzTrigger)) {
            return QuartzMessage.success();
        } else {
            return QuartzMessage.failed();
        }


    }

    @RequestMapping(value = "/rescheduleJob", method = RequestMethod.GET)
    @ResponseBody
    public QuartzMessage rescheduleJob(@RequestParam("jobName") String jobName, @RequestParam("jobGroup") String jobGroup, @RequestParam("jobCron") String jobCron) {
        logger.info("url:rescheduleJob");
        if (quartzService.rescheduleJob(jobName, jobGroup, jobCron)) {
            return QuartzMessage.success();
        } else {
            return QuartzMessage.failed();
        }
    }

    @RequestMapping(value = "/deleteJob", method = RequestMethod.GET)
    @ResponseBody
    public QuartzMessage deleteJob(@RequestParam("jobName") String jobName, @RequestParam("jobGroup") String jobGroup) {
        logger.info("url:deleteJob");
        if (quartzService.deleteJob(jobName, jobGroup)) {
            return QuartzMessage.success();
        } else {
            return QuartzMessage.failed();
        }
    }

    @RequestMapping(value = "/pauseJob", method = RequestMethod.GET)
    @ResponseBody
    public QuartzMessage pauseJob(@RequestParam("jobName") String jobName, @RequestParam("jobGroup") String jobGroup) {
        logger.info("url:pauseJob");
        if (quartzService.pauseJob(jobName, jobGroup)) {
            return QuartzMessage.success();
        } else {
            return QuartzMessage.failed();
        }
    }

    @RequestMapping(value = "/resumeJob", method = RequestMethod.GET)
    @ResponseBody
    public QuartzMessage resumeJob(@RequestParam("jobName") String jobName, @RequestParam("jobGroup") String jobGroup) {
        logger.info("url:resumeJob");
        if (quartzService.resumeJob(jobName, jobGroup)) {
            return QuartzMessage.success();
        } else {
            return QuartzMessage.failed();
        }
    }

    @RequestMapping(value = "/triggerJob", method = RequestMethod.GET)
    @ResponseBody
    public QuartzMessage triggerJob(@RequestParam("jobName") String jobName, @RequestParam("jobGroup") String jobGroup) {
        logger.info("url:triggerJob");
        if (quartzService.triggerJob(jobName, jobGroup)) {
            return QuartzMessage.success();
        } else {
            return QuartzMessage.failed();
        }
    }


}
